package com.cy.ppj.web.admin.company;

import com.cy.ppj.model.PageBean;
import com.cy.ppj.model.Result;
import com.cy.ppj.model.ao.company.CompanyAddAO;
import com.cy.ppj.model.ao.company.CompanyEditAO;
import com.cy.ppj.model.ao.user.CompanyMemberAddAO;
import com.cy.ppj.model.domian.company.Company;
import com.cy.ppj.model.enums.CompanyStatus;
import com.cy.ppj.model.query.ChannelUserByCompanyAdminQuery;
import com.cy.ppj.model.query.CompanyAdminQuery;
import com.cy.ppj.model.query.CompanyTeamMemberAdminQuery;
import com.cy.ppj.model.query.OrderAdminByTypeQuery;
import com.cy.ppj.model.vo.ChannelUserByCompanyAdminVO;
import com.cy.ppj.model.vo.CompanyAdminVO;
import com.cy.ppj.model.vo.OrderAdminByTypeVO;
import com.cy.ppj.model.vo.admin.company.CompanyTeamMemberAdminVO;
import com.cy.ppj.service.company.CompanyService;
import com.cy.ppj.service.order.OrderService;
import com.cy.ppj.service.user.UserService;
import com.cy.ppj.web.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @description: 公司管理
 * @author: Ocean
 * @create: 2018-08-03 17:27
 **/
@Slf4j
@RestController
@Api(tags = "后台-渠道公司管理")
@RequestMapping("/admin/company")
public class AdminCompanyController extends BaseController {

    @Autowired
    private CompanyService companyService;
    @Autowired
    private UserService userService;
    @Autowired
    private OrderService orderService;

    @ApiOperation("渠道公司列表")
    @GetMapping("/list")
    public PageBean<CompanyAdminVO> list(CompanyAdminQuery query) {
    	PageBean<CompanyAdminVO> pageBean = companyService.selectCompanyAdminList(query);
    	return pageBean;
    }
    
    @ApiOperation("添加公司")
    @PostMapping("/add")
    public Result add(@Validated CompanyAddAO companyAddAO, BindingResult bindingResult) {

        this.checkError(bindingResult);
        companyService.addCompany(companyAddAO);
        return ok();
    }

    @ApiOperation("编辑公司")
    @PostMapping("/edit")
    public Result Detail(@Validated CompanyEditAO companyEditAO, BindingResult bindingResult) {
    	this.checkError(bindingResult);
        companyService.updateCompany(companyEditAO);
    	return ok();
    }
    
    
    @ApiOperation("更改状态")
    @PostMapping("/update/status")
    public Result updateStatus(@ApiParam(value = "公司ID", example = "0", required = true) @RequestParam Long companyId,
    		@ApiParam(value = "公司状态", required = true) @RequestParam CompanyStatus companyStatus) {
    	companyService.updateCompanyStatus(companyId, companyStatus);
    	return ok();
    }
    
    @ApiOperation("渠道员（经纪人）列表")
    @GetMapping("/channel/list")
    public PageBean<ChannelUserByCompanyAdminVO> channelList(ChannelUserByCompanyAdminQuery query) {
    	PageBean<ChannelUserByCompanyAdminVO> pageBean = userService.queryChannelUserByCompanyAdmin(query);
    	return pageBean;
    }
    
    @ApiOperation("设置管理员")
    @PostMapping("/set/director")
    public Result setDirector(@ApiParam(value = "公司ID", example = "0", required = true) @RequestParam Long companyId,
    		@ApiParam(value = "渠道员用户ID", required = true) @RequestParam Long channelUserId) {
    	companyService.setDirector(companyId, channelUserId);
    	return ok();
    }
    
    @ApiOperation("渠道公司-团队订单明细")
    @GetMapping("/order/list")
    public PageBean<OrderAdminByTypeVO> orderList(OrderAdminByTypeQuery query) {
    	query.setType(3);
    	PageBean<OrderAdminByTypeVO> pageBean = orderService.queryOrderAdminByType(query);
    	return pageBean;
    }
    
    @ApiOperation("新增渠道员")
    @PostMapping("/add/channel")
    public Result addMemberUser(@Validated CompanyMemberAddAO companyMemberAddAO, BindingResult bindingResult) {
    	this.checkError(bindingResult);
    	companyService.addMemberUser(companyMemberAddAO);
    	return ok();
    }
    
    @ApiOperation("全部的渠道公司列表，不分页，有缓存")
    @GetMapping("/all/list")
    public Result<Company> allListInCache() {
    	List<Company> list = companyService.selectAllCompanyListInCache();
    	return ok("", list);
    }
    
    @ApiOperation("团队成员列表")
    @GetMapping("/team/member/list")
    public PageBean<CompanyTeamMemberAdminVO> selectCompanyTeamMemberAdminList(@Validated CompanyTeamMemberAdminQuery query, BindingResult bindingResult) {
    	this.checkError(bindingResult);
    	PageBean<CompanyTeamMemberAdminVO> pageBean = companyService.selectCompanyTeamMemberAdminList(query);
    	return pageBean;
    }
    
    
}
